home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_oakplank.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  6.9 KB  |  174 lines

  1. /****************************************************************************
  2.  * oakplank.sl
  3.  *
  4.  * Description: makes procedural varnished wood planks.  The planks
  5.  *    are projected onto the x-y plane, with the length aligned with
  6.  *    the y axis.  The subpattern within each individual plank is just
  7.  *    a shifted version of the oaktexture function from oak.h.
  8.  *   
  9.  * Parameters for the coordinate mapping: 
  10.  *   shadingspace - space in which the pattern is laid out
  11.  *   shadingfreq - overall scaling factor for the pattern
  12.  *   Pref - if supplied, gives the reference pose
  13.  *
  14.  * Parameters for the pattern of the plank structure: 
  15.  *   plankwidth, planklength - size of the planks
  16.  *   groovewidth, grooveheight - width of the grooves between planks
  17.  *   Cgroove - color of the grooves between the planks
  18.  *   groovedepth - how far down do the grooves displace?
  19.  *   edgewidth - how close to the plank border does the wood start to curl?
  20.  *   varyhue, varysat, varylum - control plank-to-plank color variation
  21.  * 
  22.  * Parameters for the color and pattern of the wood grain: 
  23.  *   Clightwood - the light, "background" wood color
  24.  *   Cdarkwood - the darker color in the ring/grain
  25.  *   ringfreq - mean frequency of ring spacing
  26.  *   ringunevenness - 0=equally spaced rings, larger is unequally spaced
  27.  *   grainfreq - frequency of the fine grain
  28.  *   ringnoise, ringnoisefreq - general warping of the domain
  29.  *   trunkwobble, trunkwobblefreq - controls noise which wobbles the
  30.  *       axis of the trunk so that it's not perfectly on the z axis.
  31.  *   angularwobble, angularwobblefreq - warping indexed by angle about
  32.  *       the z axis.
  33.  *   ringy, grainy - overall scale on the degree to which rings and
  34.  *       grain are weighted.  0 turns one off, 1 makes full effect.
  35.  *   divotdepth - depth (in shader units) of the displacement due to
  36.  *       ring or grain.
  37.  *   truedisp - 1 for true displacement, 0 for bump mapping
  38.  *
  39.  * Parameters for illumination model:
  40.  *   Ka, Kd, Ks, roughness - the usual meaning
  41.  *   Kr, blur, eta - reflection parameters for the tile
  42.  *   envname, envspace, envrad - environment mapping controls
  43.  *   rayjitter, raysamples - ray tracing controls
  44.  *   varnishlump, arnishlumpfreq - amp & freq of lumpiness in the varnish
  45.  *
  46.  ***************************************************************************
  47.  *
  48.  * Author: Larry Gritz, 1999
  49.  *
  50.  * Contacts:  lg@pixar.com
  51.  *
  52.  * $Revision: 1.1 $    $Date: 2002/11/25 20:24:00 $
  53.  *
  54.  ****************************************************************************/
  55.  
  56. /* Comment out the following line if you do *not* wish to use BMRT and
  57.  * PRMan together.
  58.  */
  59. #include "k3d_rayserver.h"
  60.  
  61. #include "k3d_project.h"
  62. #include "k3d_pshad.h"
  63. #include "k3d_material.h"
  64. #include "k3d_noises.h"
  65. #include "k3d_displace.h"
  66. #include "k3d_patterns.h"
  67.  
  68. #include "k3d_oak.h"
  69.  
  70.  
  71. /* Given 2-D texture coordinates ss,tt, filter widths ds, dt, and the
  72.  * width and height of the grooves between tiles, figure out which
  73.  * (integer indexed) plank we are on and what coordinates within our
  74.  * individual plank we are shading.
  75.  */
  76. float plankpattern(float ss, tt, ds, dt;
  77.            float plankwidth, planklength;
  78.            float groovewidth, grooveheight;
  79.            output float swhichplank, twhichplank;
  80.            output float splank, tplank;)
  81. {
  82.   /* Find which s plank we're on and our s coordinate within it */
  83.   swhichplank = floor(ss / plankwidth);
  84.   splank = ss - swhichplank * plankwidth;
  85.   /* Shift in t a random amount for each plank column */
  86.   float newt = tt + planklength * cellnoise(swhichplank);
  87.   /* Find which t plank we're on and our t coordinate within it */
  88.   twhichplank = floor(newt / planklength);
  89.   tplank = newt - twhichplank * planklength;
  90.   /* Calculate our "in-plank" value by multiplying two perpendicular
  91.    * filtered pulsetrain functions.
  92.    */
  93.   return filteredpulsetrain(groovewidth, plankwidth, ss + groovewidth / 2,
  94.                 ds) * filteredpulsetrain(grooveheight,
  95.                              planklength,
  96.                              newt + grooveheight / 2,
  97.                              dt);
  98. }
  99.  
  100.  
  101.  
  102. surface k3d_oakplank(float Ka = 1, Kd = 1, Ks = .75, roughness = 0.1;
  103.              float Kr = 1, blur = 0, eta = 1.5;
  104.              DECLARE_DEFAULTED_ENVPARAMS;
  105.              DEFAULT_PSHAD_PARAMS;
  106.              float ringfreq = 8, ringunevenness = 0.5;
  107.              float ringnoise = 0.02, ringnoisefreq = 1;
  108.              float grainfreq = 25;
  109.              float trunkwobble = 0.15, trunkwobblefreq = 0.025;
  110.              float angularwobble = 1, angularwobblefreq = 1.5;
  111.              float divotdepth = 0.012, truedisp = 0;
  112.              color Clightwood = color(.5, .2, .067);    /* light wood color */
  113.              color Cdarkwood = color(0.15, 0.077, 0.028);
  114.              color Cgroove = color(0.02, 0.02, 0.02);
  115.              float ringy = 1, grainy = 1;
  116.              float plankwidth = 2, planklength = 30;
  117.              float groovewidth = 0.05, grooveheight = 0.05;
  118.              float varyhue = 0.015, varysat = 0.1, varylum = 0.5;
  119.              float groovedepth = 0.03, edgewidth = 0.1;
  120.              float varnishlump = 0.01, varnishlumpfreq = 0.5;
  121.   )
  122. {
  123.   GET_PSHAD;
  124.   float ss = xcomp(Pshad), tt = ycomp(Pshad), height = zcomp(Pshad);
  125.   float dss = filterwidth(ss), dtt = filterwidth(tt);
  126.  
  127.   /*
  128.    * Find out where in the pattern we are: which plank we're on, and
  129.    * the (splank,tplank) coordinates (both on [0,1]) within our tile.
  130.    */
  131.   float swhichplank, twhichplank, splank, tplank;
  132.   float inplank = plankpattern(ss, tt, dss, dtt, plankwidth, planklength,
  133.                    groovewidth, grooveheight,
  134.                    swhichplank, twhichplank, splank, tplank);
  135.   float plankindex = swhichplank + 13 * twhichplank;
  136.   point Ppat =
  137.     point(splank - 0.5, height - 0.01 * tplank, tplank) + vector(1, 5,
  138.                                  10) *
  139.     (vector cellnoise(swhichplank, twhichplank) - 0.5);
  140.  
  141.   float wood = oaktexture(Ppat, dPshad, ringfreq, ringunevenness, grainfreq,
  142.               ringnoise, ringnoisefreq, trunkwobble,
  143.               trunkwobblefreq, angularwobble,
  144.               angularwobblefreq, ringy, grainy);
  145.   color Cwood = mix(Clightwood, Cdarkwood, wood);
  146.  
  147.   Cwood = varyEach(Cwood, plankindex, varyhue, varysat, varylum);
  148.   Cwood = mix(Cgroove, Cwood, inplank);
  149.  
  150.   /* Displacement: the edges of the planks displace down a bit, as do
  151.    * the grooves between planks. 
  152.    */
  153.   float edgedisp = smoothpulse(0, edgewidth, plankwidth - edgewidth,
  154.                    plankwidth, splank);
  155.   edgedisp *=
  156.     smoothpulse(0, edgewidth, planklength - edgewidth, planklength, tplank);
  157.   normal Nf = faceforward(normalize(N), I);
  158.   float disp = -wood * divotdepth + groovedepth * (edgedisp - 1);
  159.   disp +=
  160.     varnishlump * filteredsnoise(Pshad * varnishlumpfreq,
  161.                  dPshad * varnishlumpfreq);
  162.   Nf = faceforward(Displace(Nf, "shader", disp, truedisp), I);
  163.  
  164.   /* Illumination model
  165.    * Less specular in the grooves, more specular in the dark wood. 
  166.    */
  167.   float specadjusted = 1 + .3 * wood - 0.8 * (1 - inplank);
  168.   Ci =
  169.     MaterialShinyPlastic(Nf, Cwood, Ka, Kd, specadjusted * Ks, roughness,
  170.              specadjusted * Kr, blur, eta, 1, ENVPARAMS);
  171.   Oi = Os;
  172.   Ci *= Oi;
  173. }
  174.